//index.js
//获取应用实例
var app = getApp()
var day = ["今天", "明天", "后天"];
Page({
  data: {
    day: day,
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  onLoad: function () {
    // 判断是否授权定位
    this.getPermission();

  },
    //获取用户地理位置权限
    getPermission:function(obj){
        var that = this;
        wx.chooseLocation({
            success: function (res) {
                var latitude = res.latitude;
                var longitude = res.longitude;
                //赋值经纬度
                that.getCity(latitude, longitude);
            },
            fail:function(){
                wx.getSetting({
                    success: function (res) {
                        if (!res.authSetting['scope.userLocation']) {
                            /*that.showModal({
                                title: '',
                                content: '请允许****获取您的定位',
                                confirmText: '授权',
                                success: function (res) {
                                    if (res.confirm) {

                                        that.openSetting();
                                    } else {
                                        console.log('get location fail');
                                    }
                                }
                            })*/
                        }else {
                            /*//用户已授权，但是获取地理位置失败，提示用户去系统设置中打开定位
                            that.showModal({
                                title: '',
                                content: '请在系统设置中打开定位服务',
                                confirmText: '确定',
                                success: function (res) {
                                }
                            })*/
                        }
                    }
                })
            }
        })
    },

    //获取当前位置的经纬度
    getLocation: function () {
      var that = this;
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          var latitude = res.latitude
          var longitude = res.longitude
          console.log("lat:" + latitude + ",lon:" + longitude)

          that.getCity(latitude, longitude);
        },
        fail: function (res) {
        },
        complete: function (res) { },
      })
    },

  //获取城市信息
  getCity: function (latitude, longitude) {
    var that = this;
    var url = "https://api.map.baidu.com/reverse_geocoding/v3/";
    var params = {
      ak: "4ABzr9G1gHutpt3iPNQMRdST6a7iRkux",
      output: "json",
      location: latitude + "," + longitude
    };

    wx.request({
      url: url,
      data: params,
      success: function (res) {
        console.log(res);
          var city = res.data.result.addressComponent.city;
          var district = res.data.result.addressComponent.district;
          var street = res.data.result.addressComponent.street;
        that.setData({
          city: city,
          district: district,
          street: street
        })
        //获取城市名
        var descCity = city.substring(0, city.length - 1);
        that.getWeather(descCity);
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  //获取天气预报
  getWeather: function (city) {
    var that = this;
    var url = "https://free-api.heweather.com/v5/weather";
    var params = {
      city: city,
      key: "894fc2a749104d679fa022c3e71afe83"
    }

    wx.request({
      url: url,
      data: params,
      success: function (res) {
        var tmp = res.data.HeWeather5[0].now.tmp;
        var txt = res.data.HeWeather5[0].now.cond.txt;
        var code = res.data.HeWeather5[0].now.cond.code;
        var qlty = res.data.HeWeather5[0].aqi.city.qlty;
        var dir = res.data.HeWeather5[0].now.wind.dir;
        var sc = res.data.HeWeather5[0].now.wind.sc;
        var hum = res.data.HeWeather5[0].now.hum;
        var fl = res.data.HeWeather5[0].now.fl;
        var daily_forecast = res.data.HeWeather5[0].daily_forecast;
        that.setData({
          tmp: tmp,
          txt: txt,
          code: code,
          qlty: qlty,
          dir: dir,
          sc: sc,
          hum: hum,
          fl: fl,
          daily_forecast: daily_forecast
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  }

})


